package com.ylr.send.framework.controller.web;

import java.util.List;
import java.util.Objects;

import com.ylr.send.framework.service.SmsTemplateFlowService;
import com.ylr.base.common.annotation.Resubmit;
import com.ylr.base.common.dto.PageDto;
import com.ylr.send.common.dto.SmsTemplateFlowDto;
import com.ylr.base.common.jsr303.group.Search;
import com.ylr.base.common.tool.Result;
import com.ylr.send.common.vo.SmsTemplateFlowFormVo;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;


/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-06-14 21:21:36
 * className: SmsTemplateFlowWebController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/web/sms-template-flow")
@AllArgsConstructor
public class SmsTemplateFlowWebController {

    private static final Logger log = LoggerFactory.getLogger(SmsTemplateFlowWebController.class);

    private final SmsTemplateFlowService smsTemplateFlowService;

    /**
     * 分页查询短信模板流水接口
     * @param smsTemplateFlowFormVo 参数对象
     * @return 短信模板流水分页数据
     */
    @GetMapping("/page/list")
    public Result<PageDto<SmsTemplateFlowDto>> pageSmsTemplateFlowList(@Validated(Search.class) @RequestBody SmsTemplateFlowFormVo smsTemplateFlowFormVo) {
        log.info("web => 开始分页查询短信模板流水操作。{}", smsTemplateFlowFormVo);

        PageDto<SmsTemplateFlowDto> pageDto = smsTemplateFlowService.pageSmsTemplateFlowList(smsTemplateFlowFormVo);

        log.info("web => 分页查询短信模板流水操作结束");
        return Result.success(pageDto);
    }

    /**
     * 获取短信模板流水接口
     * @param smsTemplateId 短信模板主键
     * @param version       版本号
     * @return 短信模板流水
     */
    @GetMapping("/get")
    public Result<SmsTemplateFlowDto> getSmsTemplateFlow(@RequestParam("smsTemplateId") Long smsTemplateId,
                                                         @RequestParam(value = "version", required = false) Integer version) {
        log.info("web => 开始获取短信模板流水操作。smsTemplateId={} version={}", smsTemplateId, version);

        SmsTemplateFlowDto smsTemplateFlowDto = smsTemplateFlowService.getSmsTemplateFlow(smsTemplateId, version);

        log.info("web => 获取短信模板流水操作结束");
        return Result.success(smsTemplateFlowDto);
    }

    /**
     * 删除短信模板流水接口
     * @param idList 主键列表
     */
    @Resubmit(spEL = "'deleteSmsTemplateFlow'.concat(#userPo.id)")
    @DeleteMapping("/delete")
    public Result<Object> deleteSmsTemplateFlow(@Size(min = 1, message = "主键列表不能为空") @RequestBody List<Long> idList) {
        log.info("web => 开始删除短信模板流水操作。idList={}", idList);

        int length = idList.size();
        for (int i = 0; i < length; i++) {
            if (Objects.isNull(idList.get(i))) {
                return Result.requestFail("第" + (i + 1) + "个主键值为空，请求失败");
            }
        }

        smsTemplateFlowService.deleteSmsTemplateFlow(idList);

        log.info("web => 删除短信模板流水操作结束");
        return Result.success();
    }

}
